#pragma once

#include <iostream>
#include <unistd.h>
#include <time.h>
#include <stdarg.h>

const std::string Path = "./Log/a";

enum
{
    Debug = 1,
    Info,
    Warning,
    Fatal,
    Error,
    Unknown
};

std::string ToLevelString(int level)
{
    switch (level)
    {
    case Debug:
        return "Debug";
        break;
    case Info:
        return "Info";
        break;
    case Warning:
        return "Warning";
        break;
    case Fatal:
        return "Fatal";
        break;
    case Error:
        return "Debug";
        break;
    default:
        return "Unknown";
        break;
    }
}

std::string GetTime()
{
    char buffer[1024];
    time_t tm;
    time(&tm);
    struct tm *tmp = localtime(&tm);
    snprintf(buffer, sizeof(buffer), "%d-%d-%d %d:%d:%d", tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
    return buffer;
}

void LogMessage(int level, const char *format, ...)
{
    char LeftBuffer[1024];
    snprintf(LeftBuffer, sizeof(LeftBuffer), "[%s] [%s] [%d] ", ToLevelString(level).c_str(), GetTime().c_str(), getpid());

    char RightBuffer[1024];
    va_list va;
    va_start(va, format);
    vsnprintf(RightBuffer, sizeof(RightBuffer), format, va);
    va_end(va);

    FILE* fd = fopen(Path.c_str(),"a");
    fprintf(fd,"%s%s\n",LeftBuffer,RightBuffer);

    fclose(fd);
}


